package cn.fastoo.web.backstage.web.controller.product;

import cn.fastoo.common.dao.base.BaseMeasurementDao;
import cn.fastoo.common.dao.product.ProductBrandDao;
import cn.fastoo.common.dao.product.ProductDao;
import cn.fastoo.common.dao.product.query.ViewProductCategoryDao;
import cn.fastoo.common.entity.base.BaseMeasurement;
import cn.fastoo.common.entity.product.Product;
import cn.fastoo.common.entity.product.ProductBrand;
import cn.fastoo.common.entity.product.Query.ViewProductCategory;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroup;
import cn.fastoo.common.entity.product.Query.ViewProductCategoryAttrgroupAttr;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * Created by lowkey on 17/10/11.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductDao dao;
    @Autowired
    private ProductBrandDao brandDao;
    @Autowired
    private ViewProductCategoryDao viewProductCategoryDao;
    @Autowired
    private BaseMeasurementDao measurementDao;

    @RequestMapping
    public String index() {
        return "product/index";
    }
    @RequestMapping("all")
    public String all() {
        return "product/product";
    }
    @RequestMapping("detail")
    public String detail() {
        return "product/detail";
    }

    @RequestMapping("detail-pic")
    public String pic() {
        return "product/detail-pic";
    }

    @RequestMapping("/manager")
    @ResponseBody
    public String manager(@ModelAttribute Product newData,String json, QuerySearch querySearch, String op) {
        Search search = querySearch.getSearch();
        if(StringUtils.isNotBlank(op)){
            if("add".equals(op)){
                dao.insert(newData);
                //logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
            }else {
                Product oldData = dao.findById(newData.getId());
                if(oldData==null){
                    return JsonResult.success("错误参数,请检查");
                }
                oldData.setName(newData.getName());
                oldData.setAttrJson(newData.getAttrJson());
                oldData.setCode(newData.getCode());
                oldData.setPrice(newData.getPrice());
                oldData.setIsPackage(newData.getIsPackage());
                oldData.setUnit(newData.getUnit());
                oldData.setIsGlobalstock(newData.getIsGlobalstock());
                oldData.setQuantity(newData.getQuantity());
                oldData.setProductTypeId(newData.getProductTypeId());
                oldData.setMerchantBarCode(newData.getMerchantBarCode());
                oldData.setMerchantCode(newData.getMerchantCode());
                oldData.setProductModel(newData.getProductModel());
                oldData.setWholesalePrice(newData.getWholesalePrice());
                oldData.setPurchasePrice(newData.getPurchasePrice());
                oldData.setSellPoint(newData.getSellPoint());
                oldData.setShopPrice(newData.getShopPrice());
                oldData.setIsCod(newData.getIsCod());
                oldData.setIsWarranty(newData.getIsWarranty());
                oldData.setIsInvoice(newData.getIsInvoice());
                oldData.setPackageWeight(newData.getPackageWeight());
                oldData.setPostFee(newData.getPostFee());
                oldData.setEmsFee(newData.getEmsFee());
                oldData.setExpressFee(newData.getExpressFee());
                oldData.setPackageContent(newData.getPackageContent());
                oldData.setPackageLength(newData.getPackageLength());
                oldData.setPackageWidth(newData.getPackageWidth());
                oldData.setPackageHeight(newData.getPackageHeight());
                oldData.setProductBrandId(newData.getProductBrandId());
                oldData.setMeasurementId(newData.getMeasurementId());
                if("save".equals(op)){
                    dao.update(oldData);
                }else if("del".equals(op)){
                    dao.delete(oldData);
                }
            }
            return JsonResult.success("操作成功");
        }
        return JsonResult.querySearchSuccess(dao.find(search),querySearch);
    }

    @RequestMapping("/info")
    @ResponseBody
    public String info(@RequestParam  Long id){
        Search search = new Search();
        JSONObject result = new JSONObject();
        Product product = dao.findById(id);
        List<ProductBrand> productBrand = brandDao.find(search);
        List<BaseMeasurement> baseMeasurements =measurementDao.find(search);
        result.put("info",product);
        result.put("attr",viewProductCategoryDao.getById(product.getProductCategoryId()));
        result.put("brand",productBrand);
        result.put("measurement",baseMeasurements);
        return JsonResult.success(result);
    };

    @RequestMapping("/getSaleAttr")
    @ResponseBody
    public String getSaleattr(@RequestParam  Long id){
        Product product = dao.findById(id);
        List<ViewProductCategoryAttrgroupAttr> viewProductCategory = viewProductCategoryDao.getSaleAttrById(product.getProductCategoryId());
        return JsonResult.success(viewProductCategory);
    };
    @RequestMapping("/updateMainPic")
    @ResponseBody
    public String updateMainPic(@RequestParam  Long id,@RequestParam String url){
        Product product = dao.findById(id);
        product.setMainPicUrl(url);
        dao.update(product);
        return JsonResult.success("ok");
    };
    @RequestMapping("/updatePics")
    @ResponseBody
    public String updatePics(@RequestParam  Long id,@RequestParam String url){
        Product product = dao.findById(id);
        String[] urlList = url.split(",");
        String maiUrl;
        String picUrl="";
        maiUrl = urlList[0];
        for(int i=1;i<urlList.length;i++){
            picUrl +=urlList[i]+",";
        }
        if(StringUtils.isNotEmpty(picUrl)){
            picUrl = picUrl.substring(0,picUrl.length()-1);
        }
        product.setMainPicUrl(maiUrl);
        product.setPicUrls(picUrl);
        dao.update(product);
        return JsonResult.success("ok");
    };

    @RequestMapping("/updateHtmlContent")
    @ResponseBody
    public String updateHtmlContent(@RequestParam  Long id,@RequestParam String content,@RequestParam boolean isPc){
        Product product = dao.findById(id);
        if(isPc){
            product.setHtmlContent(content);
        }else{
            product.setMobileHtmlContent(content);
        }
        dao.update(product);
        return JsonResult.success("ok");
    };




}
